﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using ankietyzacja.Dao;

namespace ankietyzacja
{
    public partial class Mailing : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }


        protected void EmailsGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            String connectionString = ConfigurationManager.ConnectionStrings["ankietyzacjaConnectionString"].ConnectionString;

            int emailId = (int)EmailsGridView.DataKeys[e.RowIndex].Value;

            SqlConnection connection = new SqlConnection(connectionString);

            SqlTransaction transaction;

            connection.Open();
            transaction = connection.BeginTransaction();

            MailingDao mailingDao = new MailingDao(connection, transaction);

            mailingDao.usunWyslanaAnkieta(emailId);

            mailingDao.usunUzytkownikXEmail(emailId);

            transaction.Commit();
            connection.Close();
        }

        protected void EmailsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {


            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                LinkButton DeleteLinkButton = (LinkButton)e.Row.FindControl("DeleteLinkButton");

                Label DataWyslaniaLabel = (Label)e.Row.FindControl("DataWyslaniaLabel");


                if (DataWyslaniaLabel.Text == "")
                {
                    DeleteLinkButton.Visible = true;
                }
                else
                {
                    DeleteLinkButton.Visible = false;
                }
            }
        }

        protected void NewEmailButton_Click(object sender, EventArgs e)
        {
            String connectionString = ConfigurationManager.ConnectionStrings["ankietyzacjaConnectionString"].ConnectionString;

            SqlConnection connection = new SqlConnection(connectionString);

            connection.Open();

            MailingDao mailingDao = new MailingDao(connection, null);


            int EmailId = mailingDao.insertNewEmail();


            connection.Close();

            Server.Transfer("Email.aspx?email_id=" + EmailId, true);


        }


    }
}